<?php
/**
 * 砍价活动订单列表
 * User: Administrator
 * Date: 2017/7/14
 * Time: 16:42
 */
defined('InNcPHP') or exit('Access Invalid!');

class bargain_orderControl extends SystemControl {

    private $links = array(
        array('url'=>'act=bargain_order&op=index','lang'=>'nc_manage')
    );

    public  function __construct()
    {
        parent::__construct();
        Language::read('bargain_order');
    }

    public  function indexOp(){
        $bargain_order_model = Model('bargain_order');
        $merchant_model = Model('member_merchant');
        $table = 'bargain_order,member,member_merchant';
        $on = 'member.member_id=bargain_order.member_id,member_merchant.merchant_id=bargain_order.merchant_id';
        $field = array(
            'bargain_order.bg_order_id',
            'bargain_order.bg_order_sn',
            'bargain_order.bg_title',
            'bargain_order.add_time',
            'bargain_order.merchant_id',
            'bargain_order.bg_order_status',
            'bargain_order.bg_order_price',
            'bargain_order.type',
            'member.nickname',
            'member_merchant.mc_shop_name'


        );
        //查询条件
        $condition = array();

        if (isset($_GET['search_sn']) && $_GET['search_sn']) {
            $condition['bg_order_sn'] = $_GET['search_sn'];
        }
        $time_from = strtotime($_GET['search_stime']);
        $time_to = strtotime($_GET['search_etime']);
        if($time_from && $time_to){
            $condition['add_time'] = array('between',array($time_from,$time_to));
        }elseif($time_from){
            $condition['add_time'] = array('egt',$time_from);
        }elseif($time_to){
            $condition['add_time'] = array('elt',$time_to);
        }
        if (isset($_GET['search_merchant']) && is_numeric($_GET['search_merchant'])) {
            $condition['bargain_order.merchant_id'] = intval($_GET['search_merchant']);
        }
        if (isset($_GET['search_status']) && is_numeric($_GET['search_status'])) {
            $condition['bg_order_status'] = intval($_GET['search_status']);
        }
        if(isset($_GET['order_type']) && is_numeric($_GET['order_type'])){
            $condition['type'] = intval($_GET['order_type']);

        }

        //订单列表
        $bargain_order_list = $bargain_order_model
            ->table($table)
            ->field(implode(',',$field))
            ->join('left,left')
            ->on($on)
            ->where($condition)
            ->order('add_time desc')
            ->page(10)
            ->select();
        $merchants = $merchant_model->getMerchantList(array('mc_status'=>1), 'merchant_id,mc_shop_name');//商家列表

        Tpl::output('merchants',$merchants);
        Tpl::output('bargain_order_list',$bargain_order_list);
        Tpl::output('search',$_GET);
        Tpl::output('top_link',$this->sublink($this->links,'index'));
        Tpl::output('status',$bargain_order_model->status);
        Tpl::output('type',$bargain_order_model->type);
        Tpl::output('page', $bargain_order_model->showpage());
        Tpl::showpage('bargain_order.index');
    }


    /**
     * 物流发货
     */

    public function ShippingOp(){
        $order_id = intval($_POST['bg_order_id']);
        $logistics_no   = $_POST['logistics_no'];
        $dhl = $_POST['dhl'];
        $status = '40';

        $order_model = Model('bargain_order');
        $res = $order_model->updateBgOrder(array('bg_order_id'=>$order_id),array('logistics_no'=>$logistics_no,'dhl'=>$dhl,'bg_order_status'=>$status,'logistics_time'=>time()));
        output_data($res);

    }

    /**
     * 订单详情
     */

    public function bg_orde_infoOp(){
        $bargain_order_model = Model('bargain_order');
        $merchant_model = Model('member_merchant');
        $table = 'bargain_order,member,member_merchant';
        $on = 'member.member_id=bargain_order.member_id,member_merchant.merchant_id=bargain_order.merchant_id';
        $field = array(
            'bargain_order.bg_order_id',
            'bargain_order.bg_order_sn',
            'bargain_order.bg_title',
            'bargain_order.add_time',
            'bargain_order.merchant_id',
            'bargain_order.bg_order_status',
            'bargain_order.bg_order_price',
            'bargain_order.bg_price',
            'bargain_order.address_name',
            'bargain_order.cnee_name',
            'bargain_order.cnee_tel',
            'bargain_order.type',
            'bargain_order.logistics_no',
            'bargain_order.dhl',
            'bargain_order.logistics_time',
            'member.nickname',
            'member_merchant.mc_shop_name'


        );
        $condition['bg_order_id'] = intval($_GET['bg_order_id']);
        if(empty($condition['bg_order_id'])){
            showMessage('参数有误', 'index.php?act=bargain_order&op=index');
        }
        $bargain_order_info = $bargain_order_model
            ->table($table)
            ->field(implode(',',$field))
            ->join('left,left')
            ->on($on)
            ->where($condition)
            ->page(10)
            ->find();
        $bargain_count_model = Model('bargain_count');
        $criteria['bg_order_id'] = intval($_GET['bg_order_id']);
        $criteria['paid_sunm'] = array('gt',0);
        $paid_civil  = $bargain_count_model->getCountPage($criteria,'*','','paid_sunm desc');//代付详细

        $res = $bargain_count_model->getCountPage(array('bg_order_id'=>intval($_GET['bg_order_id'])),'cuont_sunm,paid_sunm');
        $paid_sunm = '';
        foreach ($res as $k){

            $paid_sunm+= $k['paid_sunm'];
        }
        $data = $bargain_order_info['bg_price']-$bargain_order_info['bg_order_price'];
        $cuont_sunm = $data-$paid_sunm;
        Tpl::output('cuont_sunm',$cuont_sunm);//砍价总金额度
        Tpl::output('paid_sunm',$paid_sunm);//代付总金额
        Tpl::output('paid_civil',$paid_civil);//获取代付榜
        Tpl::output('bargain_order_info',$bargain_order_info);
        Tpl::output('status',$bargain_order_model->status);
        Tpl::output('type',$bargain_order_model->type);
        Tpl::showpage('bargain_order.info');

    }


    /**
     * 导出活动订单
     */

    public function exportOp(){

        $bargain_order_model = Model('bargain_order');
        $merchant_model = Model('member_merchant');
        $table = 'bargain_order,member,member_merchant';
        $on = 'member.member_id=bargain_order.member_id,member_merchant.merchant_id=bargain_order.merchant_id';
        $field = array(
            'bargain_order.bg_order_id',
            'bargain_order.bg_order_sn',
            'bargain_order.bg_title',
            'bargain_order.add_time',
            'bargain_order.merchant_id',
            'bargain_order.address_name',
            'bargain_order.cnee_name',
            'bargain_order.bg_order_status',
            'bargain_order.bg_order_price',
            'bargain_order.bg_price',
            'bargain_order.bg_floorprice',
            'member.nickname',
            'member_merchant.mc_shop_name'


        );
        //查询条件
        $condition = array();
        if (isset($_GET['search_sn']) && $_GET['search_sn']) {
            $condition['bg_order_sn'] = $_GET['search_sn'];
        }
        $time_from = $_GET['search_stime'];
        $time_to = $_GET['search_etime'];
        if($time_from && $time_to){
            $condition['add_time'] = array('between',strtotime($time_from),'elt',strtotime($time_to));
        }elseif($time_from){
            $condition['add_time'] = array('egt',strtotime($time_from));
        }elseif($time_to){
            $condition['add_time'] = array('elt',strtotime($time_to));
        }
        if (isset($_GET['search_merchant']) && is_numeric($_GET['search_merchant'])) {
            $condition['bargain_order.merchant_id'] = intval($_GET['search_merchant']);
        }
        if (isset($_GET['search_status']) && is_numeric($_GET['search_status'])) {
            $condition['bg_order_status'] = intval($_GET['search_status']);
        }
        //每个EXCEL最大条数
        $exportSize = 1000;
        if (!is_numeric($_GET['curpage'])) {
            $count = $bargain_order_model->getBgOrderCount($condition);
            $array = array();
            if ($count > $exportSize) {
                $page = ceil($count / $exportSize);
                for ($i=1; $i<=$page; $i++){
                    $limit1 = ($i -1 ) * $exportSize + 1;
                    $limit2 = $i * $exportSize > $count ? $count : $i * $exportSize;
                    $array[$i] = $limit1 . ' ~ ' . $limit2 ;
                }
                Tpl::output('list',$array);
                Tpl::showpage('export.excel');
            } else {
                $data = $bargain_order_model
                    ->table($table)->field(implode(',',$field))
                    ->join('left,left')->on($on)
                    ->where($condition)
                    ->order('bg_order_id desc')
                    ->limit($exportSize)
                    ->select();
                //var_dump($data);exit;
                //$this->export_paid($data);

                $this->createExcel($data, $bargain_order_model->status);
            }
        } else {
            $limit1 = ($_GET['curpage'] - 1) * $exportSize;
            $limit2 = $exportSize;
            $data = $bargain_order_model
                ->table($table)->field(implode(',',$field))
                ->join('left,left')->on($on)
                ->where($condition)
                ->order('bg_order_id desc')
                ->limit("$limit1,$limit2")
                ->select();

            $this->createExcel($data, $bargain_order_model->status);
        }
    }

    /**
     * 创建Excel
     * @param array $data
     * @param array $status
     */
    private function createExcel($data, $status) {

        import('libraries.excel');
        $excel_obj = new Excel();
        $excel_data = array();
        //设置样式
        $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
        //表头
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_order_sn'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_title'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_nickname'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_member_info'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_merchant'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_cnee_name'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_addtime'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_status'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_bg_price'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_bg_floorprice'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>L('order_index_price'));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>'实际价格');
        //数据
        foreach ((array)$data as $k=>$v){
            $tmp = array();
            $tmp[] = array('data'=>$v['bg_order_sn']);
            $tmp[] = array('data'=>$v['bg_title']);
            $tmp[] = array('data'=>$v['nickname']);
            $tmp[] = array('data'=>$v['address_name']);
            $tmp[] = array('data'=>$v['mc_shop_name']);
            $tmp[] = array('data'=>$v['cnee_name']);
            //$tmp[] = array('format'=>'Number','data'=>ncPriceFormat($v['need_pay']));
            $tmp[] = array('data'=>date('Y-m-d H:i:s',$v['add_time']));
            $tmp[] = array('data'=>$status[$v['bg_order_status']]);
            $tmp[] = array('data'=>$v['bg_price']);
            $tmp[] = array('data'=>$v['bg_floorprice']);
            $tmp[] = array('data'=>$v['bg_order_price']);
            $tmp[] = array('data'=>$v['bg_order_price']);
            $excel_data[] = $tmp;
        }

        $excel_data = $excel_obj->charset($excel_data,CHARSET);
        $excel_obj->addArray($excel_data);
        $excel_obj->addWorksheet($excel_obj->charset(L('order_index'),CHARSET));
        $excel_obj->generateXML($excel_obj->charset(L('order_index'),CHARSET).$_GET['curpage'].'-'.date('Y-m-d-H',time()));


    }


    /**
     * 代付记录
     */

    public function bargain_paidOp()
    {

        Tpl::showpage('bargain_paid.index');
    }



    private function export_paid($data){
        $bargain_paid_model = Model('bargain_paid');
        //$res = $bargain_paid_model->getBgpaidPage(array('bargain_order_id'=>87));

        $table = 'bargain_paid,member,bargain_order';
        $on = "member.member_id=bargain_paid.member_id,bargain_order.bg_order_id=bargain_paid.bargain_order_id";
        $field = array(
            'bargain_paid.paid_size',
            'bargain_paid.member_id',
            'bargain_paid.add_time',
            'member.nickname',
            'bargain_order.bg_order_sn',
            );
        $condition = array();
        $res = array();
        foreach ($data as $key =>$v){
            //$res = $bargain_paid_model->getBgpaidPage(array('bargain_order_id'=>$v['bg_order_id']));
            $condition['bargain_order_id'] = $v['bg_order_id'];
            $res[] = $bargain_paid_model
                ->table($table)
                ->field(implode(',',$field))
                ->join('left,left')
                ->on($on)
                ->where($condition)
                ->order('add_time desc')
                ->limit(1000)
                ->select();
            if(empty($res)){
                continue;
            }

        }
        import('libraries.excel');
        $excel_obj = new Excel();
        $excel_data = array();
        //设置样式
        $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
        $excel_data[0][] = array('styleid'=>'s_title','data'=>'活动订单号');
        $excel_data[0][] = array('styleid'=>'s_title','data'=>'代付人');
        $excel_data[0][] = array('styleid'=>'s_title','data'=>'代付金额');
        $excel_data[0][] = array('styleid'=>'s_title','data'=>'代付时间');
        $result = array();
        foreach((array)$res as $key =>$v){
            $result[]=$v[0];
        }
        foreach ((array)$result as $s=>$vo){


            $tmp[] = array('data'=>$vo['bg_order_sn']);
            $tmp[] = array('data'=>$vo['nickname']);
            $tmp[] = array('data'=>$vo['paid_size']);
            $tmp[] = array('data'=>date('Y-m-d H:i:s',$vo['add_time']));
            $excel_data[] = $tmp;


        }



        $excel_data = $excel_obj->charset($excel_data,CHARSET);

        $excel_obj->addArray($excel_data);
        $excel_obj->addWorksheet($excel_obj->charset('代付金额列表',CHARSET));
        $excel_obj->generateXML($excel_obj->charset('代付金额列表',CHARSET).$_GET['curpage'].'-'.date('Y-m-d-H',time()));
    }
}